require('../css/mine-edit.less')

// 入口函数
document.ready(function () {
  let js_input = document.querySelector("#js_input")
  let text = document.querySelector("#text")
  let sign = ""
  let nickname = ""

  //获取本地存储内的值
  let a = JSON.parse(window.localStorage.getItem("user"))
  console.log(a);


  // 1.0 点击返回
  const backBox = document.querySelector('#back')
  backBox.addEventListener('click', function () {
    history.back()
  })

  // 2.0 选择性别
  const showPicker = document.querySelector('#showPicker')
  // console.log(showPicker);
  showPicker.addEventListener('click', function () {
    weui.picker([{
      label: '男',
      value: 0
    }, {
      label: '女',
      value: 1
    }], {
      onConfirm: function (result) {
        showPicker.textContent = result[0].label
      }
    })
  })

  // 3.0 生日选择
  const showDatePicker = document.querySelector('#showDatePicker')
  let birthday = document.querySelector("#birthday")
  let str = ""
  showDatePicker.addEventListener('click', function () {
    weui.datePicker({
      start: 1990,
      end: new Date().getFullYear(),
      onConfirm: function (result) {
        console.log(result);
        result.forEach(function (v) {//遍历数组,拿到年月日
          str += v.label //年月日进行拼接
        })
        birthday.innerHTML = str
      }
    })
  })


  //省市选择
  let showCityPicker = document.querySelector("#showCityPicker")
  let cityData = document.querySelector("#cityData")
  showCityPicker.addEventListener("click", function () {
    let arr = []
    window.$http.get("/address/province", function (res) {
      if (res.status == 0) {
        res.data.forEach(function (v) {
          arr.push({
            label: v.name,
            value: v.addressId
          })
        })

        weui.picker(arr, {
          onConfirm: function (result) {
            let pro = result[0].label
            window.$http.get("/address/city/" + result[0].value, function (res) {
              let arr1 = []
              // console.log(res);
              if (res.status == 0) {
                res.data.forEach(function (v) {
                  arr1.push({
                    label: v.name,
                    value: v.addressId
                  })
                })
                setTimeout(function () {
                  weui.picker(arr1, {
                    onConfirm: function (result) {
                      let town = result[0].label
                      cityData.innerHTML = pro + " " + town
                    }
                  })
                }, 500)
              }
            })
          }
        })
      }
    })
  })

  //修改按钮注册点击事件
  let saveInfo = document.querySelector("#saveInfo")

  saveInfo.addEventListener("click", function () {
    //处理生日
    let s = birthday.innerHTML.replace("年", "-")
    let s1 = s.replace("月", "-")
    let s2 = s1.replace("日", "")
    let arr3 = s2.split("-")
    let s3 = arr3[0] + "-" + arr3[1].padStart(2, "0") + "-" + arr3[2].padStart(2, "0")

    // console.log(s3);
    //定义一个对象
    let obj = {
      imgurl: a.imgurl,
      nickname: js_input.value,
      gender: showPicker.textContent,
      birthday: s3,
      address: cityData.innerHTML.split(" "),
      sign: text.value,
      userId: a.userId
    }
    // console.log(obj);
    //请求
    window.$http.post("/users/userEdit", obj, function (res) {
      // console.log(res);
      if (res.status == 0) {
        window.$utils.tick(0, "修改成功")//添加提示框
        setTimeout(function () {
          window.history.back()
        }, 1000)
      }
    })

  })

  //页面打开渲染
  window.$http.get("/users/accountinfo?userId=" + a.userId, function (res) {
    console.log(res);

    cityData.innerHTML = res.data.address
    showPicker.textContent = res.data.gender
    js_input.value = res.data.nickname
    text.value = res.data.sign
    birthday.innerHTML = res.data.birthday.split("T")[0]
  })

})